package com.colorful.insuranceproject.mine;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.MineRequest;
import com.colorful.insuranceproject.utils.BankUtil;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/21.
 * <p>
 * 添加银行卡
 */
public class AddBankCardActivity extends TitleActivity implements TextWatcher {

    private static final int ADD_AUTHENTICATION_BANK_CARD_URL = 0X1;

    private static final int ADD_BANK_CARD_URL = 0X3;

    private MineRequest mineRequest;

    /**
     * 真实姓名
     */
    @Bind(R.id.tv_true_name)
    EditText tvTrueName;

    /**
     * 身份证号
     */
    @Bind(R.id.tv_id_card_num)
    EditText tvIdCardNum;

    /**
     * 银行卡号
     */
    @Bind(R.id.tv_bank_card_num)
    EditText tvBankCardNum;
    /**
     * 开户银行
     */
    @Bind(R.id.tv_name)
    TextView tvName;

    /**
     * 预留手机号
     */
    @Bind(R.id.tv_phone)
    EditText tvPhone;

    @Bind(R.id.tv_set)
    TextView tvSet;

    @Override
    protected int getContentView() {
        return R.layout.activity_add_bank_card;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.add_bank_card));
    }

    @Override
    protected void onViewInitialized() {
        mineRequest = new MineRequest(getHandler());
        tvBankCardNum.addTextChangedListener(this);
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            //   提交绑定银行卡
            case ADD_BANK_CARD_URL:
                tvSet.setEnabled(true);
                if (response.isSuccessful) {
                    showToast(getString(R.string.add_bank_card_success));
                    setResult(RESULT_OK);
                    finish();
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;

            //   认证银行卡
            case ADD_AUTHENTICATION_BANK_CARD_URL:
                if (response.isSuccessful) {
                    String realName = tvTrueName.getText().toString().trim();
                    String idCardNum = tvIdCardNum.getText().toString().trim();
                    String bankCardNum = tvBankCardNum.getText().toString().trim();
                    String name = tvName.getText().toString().trim();
                    String phone = tvPhone.getText().toString().trim();
                    //  认证银行卡
                    mineRequest.addBankCardUrl(realName, idCardNum, bankCardNum, name, phone, ADD_BANK_CARD_URL);
                } else {
                    tvSet.setEnabled(true);
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    /**
     * 银行卡认证
     */
    @OnClick(R.id.tv_set)
    void OnClicked() {
        String realName = tvTrueName.getText().toString().trim();
        String idCardNum = tvIdCardNum.getText().toString().trim();
        String bankCardNum = tvBankCardNum.getText().toString().trim();
        String name = tvName.getText().toString().trim();
        String phone = tvPhone.getText().toString().trim();
        if (realName.equals("") || idCardNum.equals("") || bankCardNum.equals("") || name.equals("") || phone.equals("")) {
            showToast(getString(R.string.please_compleate_infomation));
        } else {
            //  认证银行卡
            tvSet.setEnabled(false);
            mineRequest.authenticationBankCardUrl(realName, idCardNum, bankCardNum, name, phone, ADD_AUTHENTICATION_BANK_CARD_URL);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() >= 6) {
            tvName.setText(BankUtil.getNameOfBank(s.toString()));
        }
    }
}
